package cn.tedu.zuoye.controller;


import cn.tedu.zuoye.common.exception.BusinessException;
import cn.tedu.zuoye.dao.PictureMapper;

import cn.tedu.zuoye.pojo.po.Picture;
import cn.tedu.zuoye.pojo.vo.PictureVO;
import cn.tedu.zuoye.response.StatusCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api(value = "pic", tags = "图片管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/api/pictures")
@CrossOrigin
public class PictureController {

    @Autowired
    PictureMapper pictureMapper;

    @ApiIgnore
    @PostMapping("/fileUpload")
    public String fileupload(MultipartFile picFile)  {
        //文件夹路径
        String dirPath = "D:/images";
        //日期路径
        String datePath = "";
        String fileName = "";
        try {
            fileName = picFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffix;
            datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
            File dirFile = new File(dirPath + datePath);
            //判断文件夹不存在 则创建
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            //保存文件到指定文件夹  异常抛出
            picFile.transferTo(new File(dirPath + datePath + fileName));
        }catch (Exception e){
            throw new BusinessException(StatusCode.FILE_FAIL);
        }
        return datePath+fileName;
    }

    @ApiOperation("图片上传")
    @PostMapping("insert")
    public int insert(@RequestBody PictureVO PictureVO){
        log.debug("开始处理图片上传:{}",PictureVO);
        if (PictureVO.getUrl()==""){
            throw new BusinessException(StatusCode.PIC_NOTFOUND);
        }
        Picture picture = new Picture();
        //复制属性
        BeanUtils.copyProperties(PictureVO,picture);
        picture.setUrl(PictureVO.getUrl());
        picture.setDescription(PictureVO.getDescription());
        pictureMapper.insert(picture);
        log.debug("插入数据成功：{}",picture);
        return 1;
    }

    @ApiOperation("删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping ("/delete/{id:[0-9]+}")
    public String delete(@PathVariable Long id) {
        log.debug("开始处理【删除图片】的请求，参数：{}", id);
        Picture  picture = pictureMapper.selectById(id);
        if (picture == null){
            throw new BusinessException("图片不存在");
        }
        int rows = pictureMapper.deleteById(id);
        if (rows>=1){
            new File("D:/images"+picture.getUrl()).delete();
        }
        return "删除图片数为："+rows;
    }

    @ApiOperation("修改图片信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/update/{id:[0-9]+}")
    public String update(@PathVariable Long id, PictureVO pictureVO) {
        log.debug("开始处理【修改图片】的请求，参数：{}", id);
        Picture picture = pictureMapper.selectById(id);
        log.debug("查询出来的数据为：{}",picture);
        if (picture == null){
            throw new BusinessException("图片不存在");
        }
        picture.setUrl(pictureVO.getUrl());
        picture.setDescription(pictureVO.getDescription());
        log.debug("准备修改后的的数据为：{}",picture);
        int rows = pictureMapper.update(picture);
        return "修改图片数为："+rows;
    }


    @ApiOperation("查询图片合集")
    @PostMapping("/select")
    public ArrayList<Picture> selectList() {
        log.debug("开始处理【查询图片】的请求，参数：{}");
        ArrayList<Picture> list= pictureMapper.count();
        log.debug("查询出来的结果为：{}",list);
        return list;
    }

}
